<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .container {
      /* display: flex; */
      /* justify-content: space-around; */
      width: 230px;
      height: 200px;
      font-size: 30px;
    }

    .box1 {
      background: red;
      display: block;
    }

    .box2 {
      background: blue;
      display: none;
    }

    .box3 {
      background: yellow;
      display: none;
    }
  </style>
  <body>
    <div class="container">
      <button class="btn">密码登录</button>
      <button class="btn">短信登录</button>
      <button class="btn">扫码登录</button>
      <div class="box1">1</div>
      <div class="box2">2</div>
      <div class="box3">3</div>
    </div>
    <!-- <div class="container2">
      <button class="btn">按钮一</button>
      <button class="btn">按钮二</button>
      <button class="btn">按钮三</button>
      <div class="box1">1</div>
      <div class="box2">2</div>
      <div class="box3">3</div>
    </div> -->
    <script>
      var btns = document.querySelectorAll(".container .btn");
      var divs = document.querySelectorAll(".container div");
      tab(btns, divs);
      var btns2 = document.querySelectorAll(".container2 .btn");
      var divs2 = document.querySelectorAll(".container2 div");
      tab(btns2, divs2);
      function tab(btns, divs) {
        btns.forEach((btn, k) => {
          btn.onclick = function () {
            console.log(k);
            divs.forEach((div, key) => {
              if (key == k) {
                div.style.display = "block";
              } else {
                div.style.display = "none";
              }
            });
          };
        });
      }
    </script>
  </body>
</html>
